package com.studentms.web;

import com.studentms.bean.Grade;
import com.studentms.bean.Student;
import com.studentms.service.GradeService;
import com.studentms.service.StudentService;
import com.studentms.service.impl.GradeServiceImpl;
import com.studentms.service.impl.StudentServiceImpl;
import com.studentms.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 用此StudentServlet来优化多个servlet
 * 多请求走同一个servlet
 */
//处理类的路径
@WebServlet("/Educational/student/studentServlet")
public class StudentServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //怎么区分servlet,用拼接的方法
        String method = req.getParameter("method");
        if("insert".equals(method)){//添加学员
            insertStu(req, resp);
        }else if ("updateStu".equals(method)){//更新
            updateStu(req, resp);
        }else if ("findbyid".equals(method)){//主键查询
            findById(req, resp);
        }else if ("deletebyid".equals(method)){//通过主键删除学员
            deleteStu(req, resp);
        }else{
            findList(req, resp);
        }
    }
    //删除学员
    protected void deleteStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获得参数
        String sid = req.getParameter("sid");
        //2.获得service
        //根据主键查询学生信息
        StudentService service = new StudentServiceImpl();
        int i = service.deleteStudent(sid);

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            //查询全部的页面
            writer.println("<script>alert('删除成功');location.href='/Educational/student/studentServlet';</script>");
        }else{
            //注意是先发送请求,在是跳转,如果直接跳转会报错
            writer.println("<script>alert('删除失败');location.href='/Educational/student/studentServlet';</script>");
        }
    }
    //新增学员
    protected void insertStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("utf-8");
        //1.接收参数
        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idNumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");
        //2.获取service
        StudentService service = new StudentServiceImpl();
        //将参数封装到学生对象中
        Student student = new Student();
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdNumber(idNumber);
        student.setProfession(profession);
        student.setIntroduction(introduction);
        int i = service.insertStudent(student);//返回一个值
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('新增成功');location.href='/Educational/student/studentServlet';</script>");
        }else{
            //注意是先发送请求,在是跳转,如果直接跳转会报错
            writer.println("<script>alert('新增失败');location.href='/Educational/student/getGradeList';</script>");
        }
    }
    //主键查询
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获得参数
        String sid = req.getParameter("sid");
        //2.获得service
        //根据主键查询学生信息
        StudentService service = new StudentServiceImpl();
        Student student = service.findById(Integer.parseInt(sid));

        //查询年级列表
        GradeService gradeService = new GradeServiceImpl();
        List<Grade> list = gradeService.getList();

        req.setAttribute("glist",list);
        req.setAttribute("stu",student);//存值
        //3.跳转页面
        req.getRequestDispatcher("edit.jsp").forward(req,resp);//转发
    }
    //查询列表
    protected void findList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取参数
        //1.1 模糊查询条件
        String stuname = req.getParameter("stuname");
        String stuno = req.getParameter("stuno");
        String sex = req.getParameter("sex");
        //1.2 分页数据 limit 开始位置,显示条数
        //页码值(当前页码值)
        String pageIndex = req.getParameter("pageIndex");
        //如果页面没有传入pageIndex的值,则认为默认查询第一页
        int index = pageIndex==null?1:Integer.parseInt(pageIndex);
//        System.out.println(sex);

//        int sex1;
//        if (sex==null||sex==""){
//           sex1= -1;
//        }else {
//           sex1=Integer.parseInt(sex);
//        }
//        System.out.println(sex1);
        //2.调取service
        PageUtil pageUtil = new PageUtil();//分页数工具
        StudentService service = new StudentServiceImpl();
        int sex1 = (sex==null||sex.length()==0?-1:Integer.parseInt(sex));
        List<Student> students = service.getStudents(stuname,stuno,sex1,index,pageUtil.getPageSize());//指每页5条数据
        //获取总页数=总条数%每页显示的条数>0?总条数/每页显示条数+1:总条数/每页显示条数
        int total = service.total(stuname,stuno,sex1); //总条数
        pageUtil.setTotal(total);
//        int totalPages = total%5>0?total/5+1:total/5; //总页数

        //3.跳转页面
        //如果后台想要给后台传数据,一定要在后台存值的
//        req.setAttribute("stulist",students);
        pageUtil.setDataList(students);
        pageUtil.setPageIndex(index);
        //存储模糊查询条件
        req.setAttribute("stuname",stuname);
        req.setAttribute("stuno",stuno);
        req.setAttribute("sex",sex);
        //存储分页数据   //将一个分页数据放到一个对象里面
        req.setAttribute("p1",pageUtil);
//        req.setAttribute("index",index);//存储的是当前页码值
//        req.setAttribute("size",5);//每页显示的条数
//        req.setAttribute("total",total);//总条数
//        req.setAttribute("totalPages",totalPages);//总页数

        //使用转发方式,避免请求丢失
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
    //修改学员
    protected void updateStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //使用了filter过滤器,就可以不用重复使用该代码,来防止乱码了
//        req.setCharacterEncoding("utf-8");
        //1.接收参数 ,代码的复用
        String sid = req.getParameter("sid");
        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String gid = req.getParameter("gid");

        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idNumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");

        //2.获取service
        Student student = new Student();
        student.setStuId(Integer.parseInt(sid));
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdNumber(idNumber);
        student.setProfession(profession);
        student.setIntroduction(introduction);


        StudentService service = new StudentServiceImpl();
        int i = service.updateStudent(student);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('更新成功');location.href='/Educational/student/studentServlet';</script>");
        }else{
            //注意是先发送请求,在是跳转,如果直接跳转会报错
            writer.println("<script>alert('更新失败');location.href='/Educational/student/studentServlet?method=findbyid&sid="+sid+"';</script>");
        }
    }

}
